package sample.panel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import sample.dao.JDO;
import sample.dao.LowLevelAPI;
import sample.model.DatastoreFormModel;
import sample.page.ReadPage;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class DeletePanel extends Panel{
	
	public DeletePanel(String WicketID,final PageParameters pram){
		super(WicketID);
		
		final int UseDao = pram.getInt("Dao");
		
        DatastoreFormModel model= new DatastoreFormModel();
        Form<Void> DatastoreForm = new Form<Void>("DatastoreForm");
        final TextField<String> RootKeyText = new TextField<String>("Rootkey",new PropertyModel<String>(model,"Rootkey"));
        final TextField<String> EntityKeyText = new TextField<String>("EntityKey",new PropertyModel<String>(model,"EntityKey"));
        Button PreservationButton = new Button("Preservation"){
			@Override
			public void onSubmit() {
				super.onSubmit();
				
				if(RootKeyText.getInput().equals("")){
					System.out.println("ルートが空白");
				}else{
					
					if(EntityKeyText.getInput().equals("")){
						
						Key RootKey = KeyFactory.createKey("Parent",RootKeyText.getInput());
						
						if(UseDao == 0){
							JDO dao = new JDO();
							dao.DeleteEntityGroup(RootKey);
						}else{
							LowLevelAPI LowLevelDao = new LowLevelAPI();
							LowLevelDao.DeleteEntity(RootKey);
						}
						
						
						System.out.println("Rootを削除");
						
						setResponsePage(ReadPage.class,pram);
					}else{
						
						Key RootKey = KeyFactory.createKey("Parent",RootKeyText.getInput());
						Key EntityKey = KeyFactory.createKey(RootKey,"Child",EntityKeyText.getInput());
						
						if(UseDao == 0){
							JDO dao = new JDO();
							dao.DeleteChild(EntityKey);
						}else{
							LowLevelAPI LowLevelDao = new LowLevelAPI();
							LowLevelDao.DeleteEntity(EntityKey);	
						}
						System.out.println("子要素を削除");
						
						setResponsePage(ReadPage.class,pram);
					}
				}
			}
        };
        DatastoreForm.add(RootKeyText);
        DatastoreForm.add(EntityKeyText);
        DatastoreForm.add(PreservationButton);
        add(DatastoreForm);
	}
}
